/*
 * Copyright (c) 2019. Mikes. All rights reserved.
 *
 * You can contact me with sending email to bWlrZXNfMjAxMkAxNjMuY29t for more information!
 */

package com.climber.android.commonsdk.api

import com.climber.android.commonsdk.api.entity.AccessHeader
import io.ganguo.library.mvp.util.AppUtils

/**
 * Created by Mikes at 2019-11-04 16:13
 */

enum class APITokenType {
    loginTokenPreferred,
    anonymousTokenPreferred,
    noTokenNeeded;

    companion object {
        fun getAPITokenType(typeName: String): APITokenType =
            when (typeName) {
                loginTokenPreferred.name -> loginTokenPreferred
                anonymousTokenPreferred.name -> anonymousTokenPreferred
                noTokenNeeded.name -> noTokenNeeded
                else -> loginTokenPreferred
            }

        fun headers(tokenType: APITokenType = loginTokenPreferred): AccessHeader {
            val accessHeader = AccessHeader()

            val tokenAuthorization: String? = when (tokenType) {
                loginTokenPreferred ->
                    AccountToken.loginToken?.token ?: "" //fallback to anonymous token
                anonymousTokenPreferred ->
                    ""
                noTokenNeeded -> null
            }

            accessHeader.accept = "application/json"
            if (tokenAuthorization != null) {
                accessHeader.authorization = "$tokenAuthorization"
            }

            // Test Super Token
//            accessHeader.authorization = "7d343e72f91d58862916534133dfd45220b0ff92"

            accessHeader.xClientName = "android"
            accessHeader.xClientVersion = AppUtils.getAppVersionName()

            return accessHeader
        }
    }
}